<?php


namespace app\repository\Websocket;


use app\model\WebsocketErrorLogs;
use app\repository\Repository;
use Carbon\Carbon;
use support\Cache;

class BaseRepository
{
    /**
     * 验证收到的硬件数据数据结构
     * @param $data
     * @return string
     */
    public function device_data_key($data)
    {
        $key = [
            "Author",
            'InputType',
            "InputIp",
            'InputData'
        ];
        $array_keys = array_keys($data);
        foreach ($key as $i){
            if (!in_array($i,$array_keys)){
                return "缺少".$i;
            }
        }
        return '';
    }

    public static function event($channel, $input_data, $content, $message): array
    {
        return [
            "channel" => $channel,
            "input_data" => json_encode($input_data),
            "content" => json_encode($content),
            "message" => $message
        ];
    }

    /**
     * 验证收到 web 的数据结构
     * @param $data
     * @return string
     */
    public function develop_data_key($data)
    {
        $key = [
            'author',
            "input_data",
            "input_ip",
            "input_type",
            "function"
        ];
        $array_keys = array_keys($data);
        foreach ($key as $i){
            if (!in_array($i,$array_keys)){
                return "缺少".$i;
            }
        }
        return '';
    }


    /**
     * @param $author
     * @return mixed|string 根据作者返回 id
     */
    public static function getConnectionId($author)
    {
        $name = config("websocket.cache")."_connection";
        $var = Cache::get($name);
        $id = "";
        if ($var){
            $list = json_decode($var,true);
            if ($list){
                foreach ($list as $item){
                    if ($item['author'] == $author){
                        $id = $item['id'];
                    }
                }
            }
        }
        return $id;
    }

    public static function success($data = [], $msg = "操作成功",$code = 200)
    {
        return [
            "status" => true,
            "msg" => $msg,
            "data" => $data,
            'code'=>$code
        ];

    }

    public static function ok($msg = "操作成功")
    {
        return [
            "status" => true,
            "msg" => $msg,
            "data" => []
        ];

    }

    public static function error($msg = "", $code = 300,$data = [])
    {
        return [
            "status" => false,
            "msg" => $msg,
            "code" => $code,
            "data"=>$data
        ];
    }

    public static function getDeviceId():string
    {
        return "R".Carbon::now()->timestamp;
    }

    public static function websocketErrorLog($channel,$input_data="",$content="",$message="")
    {
        WebsocketErrorLogs::query()->create([
            "channel"=>$channel,
            "message"=>$message,
            "content"=>$content,
            "input_data"=>json_encode($input_data)
        ]);
    }


    /**
     * 发送给硬件的数据结构
     * @param $type //发送给的终端类型，tcp、 udp、websocket
     * @param $to_ip //发送给终端的 ip，如果是发送给硬件设备需要这个参数
     * @param $data // 发送的数据
     * @param $function
     * @return string
     */
    public function send_data($type, $function,$rid,$return_author = "",$to_ip = '', $data = [])
    {
        $data =  [
            "client_type"=>$type,
            "to_ip"=>$to_ip,
            "data"=>$data,
            'function'=>$function,
            'rid'=>$rid,
            "return_author"=>$return_author
        ];
        return json_encode($data,JSON_UNESCAPED_UNICODE );
    }




    /**
     * 收到请求返回的数据结构
     * @param $data
     * @param $message
     * @param int $code
     * @return string
     */
    protected function result_success_data($data,$rid,$function = "",$message = "请求成功",$code = 200)
    {
        $data =  [
            "data"=>$data,
            "message"=>$message,
            "code"=>$code,
            'rid'=>$rid,
            "function"=>$function

        ];
        return json_encode($data,JSON_UNESCAPED_UNICODE );
    }


    /**
     * 收到请求返回的数据结构
     * @param $message
     * @param int $code
     * @return array
     */
    protected function result_error_data($message,$rid='',$code = 400)
    {
        $data = [
            "data"=>[],
            "message"=>$message,
            "code"=>$code,
            'rid'=>$rid
        ];
        return json_encode($data,JSON_UNESCAPED_UNICODE );
    }

}